<template>
  <el-card>
    <template #header>修改密码</template>

    <!-- 主题内容 -->
    <div>
      <el-form
        :model="accountForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
        size="small"
      >
        <!-- <el-form-item label="账号名称" prop="account">
          <el-input
            type="text"
            v-model="accountForm.account"
            autocomplete="off"
            prefix-icon="iconfont icon-yonghu1"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="accountForm.oldPassword"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont point"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="accountForm.password"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont point"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="再次输入" prop="repassword">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="accountForm.repassword"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont point"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >确认提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { resePwd } from '@/api/users.js'

export default {
  data() {
    // var validateAccount = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请输入用户名'))
    //   } else {
    //     callback()
    //   }
    // }
    var validateOldPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'))
      } else if (this.accountForm.oldPassword === this.accountForm.password) {
        callback(new Error('旧密码与新密码一致!'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        // 表单的一致性规定，在验证新密码时失去焦点会触发确认密码的校验
        if (this.accountForm.repassword !== '') {
          this.$refs.ruleForm.validateField('repassword')
        }
        callback()
      }
    }
    var validateRepassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.accountForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 1.0 数据对象
      accountForm: {
        // account: '',
        oldPassword: '',
        password: '',
        repassword: ''
      },
      // 2.0 规则对象
      rules: {
        // account: [{ validator: validateAccount, trigger: 'blur' }],
        oldPassword: [{ validator: validateOldPassword, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }],
        repassword: [{ validator: validateRepassword, trigger: 'blur' }]
      },
      flag: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 这里代表校验结果全部通过
          const users = JSON.parse(window.localStorage.getItem('users') || '{}')
          const { code, msg } = await resePwd({
            member_id: users.member_id,
            access_token: users.access_token,
            oldPassword: this.accountForm.oldPassword,
            password: this.accountForm.password,
            repassword: this.accountForm.repassword
          })
          // const token = window.localStorage.getItem('token')
          // const { code, msg } = await resePwd
          // 保存用户的信息和token
          // )({
          //   member_id: this.accountForm.account,
          //   access_token: token,
          //   oldPassword: this.accountForm.oldPassword,

          // if (code === 1) {
          //   this.$message.success(msg)
          //   setTimeout(() => {
          //     this.$router.push('/login')
          //   }, 1500)
          // } else {
          //   this.$message.error(msg)
          // }
          if (code === 1) {
            this.$message.success(msg)
            // 修改密码之后要重新登录
            window.localStorage.clear()
            setTimeout(() => {
              location.reload()
            }, 1500)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  height: 100%;
  border: none;

  .el-form {
    width: 350px;
    margin-top: 20px;
  }
}
</style>
